using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using CUtil;

namespace TestCases.UI.SettingsUI
{
    public partial class MainForm : Form
    {
        private Settings settingClass;
        

        public MainForm()
        {
            InitializeComponent();
        }

        #region main
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
           		
            Application.EnableVisualStyles();
            Application.DoEvents();
            
            Application.Run(new MainForm());

        }
        #endregion

        private bool  LoadSettingClass()
        {
            object obj = null;
            bool bret = false;
            System.IO.Stream stream = null;
            try
            {
                stream = System.IO.File.Open("settings.xml", FileMode.Open);
                System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(Settings));
                obj = ser.Deserialize(stream) as Settings;
                if (obj != null)
                {
                    settingClass = obj as Settings;
                }
                bret = true;
            }
            catch (Exception exc)
            {
                //System.Diagnostics.Debug.Assert(false, "Error loading settings" , exc.Message);
                System.Diagnostics.Debug.WriteLine("Error loading settings" , exc.Message);

                //return false;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return bret;
        }
        private void SaveSettings()
        {
            System.IO.Stream stream = null;
            try
            {
                stream = System.IO.File.Open("settings.xml", FileMode.Create);
                System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(Settings));
                ser.Serialize(stream, this.settingClass);
            }
            catch { }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
        //private void button1_Click(object sender, EventArgs e)
        //{
        //    CSettingsUI.SettingForm sf = new CSettingsUI.SettingForm();
        //    if (!LoadSettingClass())
        //        this.settingClass = new Settings();

        //    sf.SelectedObject = this.settingClass;

        //    sf.ShowDialog(this);

        //    this.settingClass = sf.SelectedObject as Settings;
        //    SaveSettings();


        //}

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenDialog();
        }
        private void OpenDialog()
        {
            TestCases.StockData.ProgramContextTest pct = new TestCases.StockData.ProgramContextTest();
            CStock.IProgramContext pc = pct.ProgramContext;

            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = pc.Configuration.Path + "\\";
            //openFileDialog1.Filter = "ProgramContext (*.) ";
            openFileDialog1.Filter = "ProgramContext (*.)|*.| All files (*.*)|*.*";


            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    string txt = openFileDialog1.FileName;

                }



            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            OpenDialog();
        }
    }
    	
}